package com.turbo.boot.crawler.spider.worker.impl.quota;

import com.alibaba.fastjson.JSONObject;
import com.turbo.boot.crawler.spider.worker.BaseTurboApiCrawler;
import com.turbo.utils.net.HttpHelper;
import com.xuxueli.crawler.rundata.RunData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 波罗的海交易指数
 *
 * @author wuyn
 * @since 2024/4/17
 */
@Slf4j
@Service("tksMysteelCrawler")
public class TksMysteelCrawler extends BaseTurboApiCrawler<TksMysteelCrawlerPageParser.PageVo> {

    private static final HttpHelper httpHelper = new HttpHelper();


    private static final Map<String, String> paramsMap = new HashMap<>();
    private static final Map<String, String> paramNamesMap = new HashMap<>();

    static {
// 澳粉价格指数
        paramsMap.put("AOF", "ID00188109");
        // 港口现货
        paramsMap.put("GKXH", "ID01501398");
        // 巴西粉矿
        paramsMap.put("BXFK", "ID00274569");
        // 国产铁精粉
        paramsMap.put("GTJF", "ID00392872");
        // 青岛港PB粉
        paramsMap.put("QDPB", "ID00104207");
        // 曹妃甸港PB粉
        paramsMap.put("CFXGPB", "ID00103970");

        paramNamesMap.put("AOF", "澳粉价格指数");
        paramNamesMap.put("GKXH", "港口现货");
        paramNamesMap.put("BXFK", "巴西粉矿");
        paramNamesMap.put("GTJF", "国产铁精粉");
        paramNamesMap.put("QDPB", "青岛港PB粉");
        paramNamesMap.put("CFXGPB", "曹妃甸港PB粉");

    }


    @Override
    public void crawl() {
        boolean buildResult = build();
        if (!buildResult) {
            return;
        }

        String url = xxlCrawlerBuilder.build().getRunData().getUrl();
        for (String key : paramsMap.keySet()) {
            try {
                String result = httpHelper.get(url + "?indexCode=" + paramsMap.get(key));
                if (StringUtils.isNotEmpty(result)) {

                    JSONObject jsonObject = JSONObject.parseObject(result);
//                    jsonObject.put("result", result);
                    jsonObject.put("key", key);
                    jsonObject.put("name", paramNamesMap.get(key));
                    this.parse(jsonObject);
                }
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }


//    public boolean build() {
//        xxlCrawlerBuilder.setUrls("https://seo.mysteel.com/semapi/searchSemIndex/getValueBycode");
//        return true;
//    }


    @Override
    public String remoteCall(RunData runData) {
        return null;
    }


    @Override
    public void parse(JSONObject jsonObject) {

        String xzValueStr = jsonObject.getJSONObject("data").getString("xzValue");
        List<String> xzValues = JSONObject.parseArray(xzValueStr, String.class);

        String yzValueStr = jsonObject.getJSONObject("data").getString("yzValue");
        List<String> yzValues = JSONObject.parseArray(yzValueStr, String.class);

        TksMysteelCrawlerPageParser.PageVo pageVo = new TksMysteelCrawlerPageParser.PageVo();

        pageVo.setQuotaCode(jsonObject.getString("key"));
        pageVo.setQuotaName(jsonObject.getString("name"));
        pageVo.setXzValues(xzValues);
        pageVo.setYzValues(yzValues);

        getPageParser().parse(pageVo);
    }


    @Override
    public void parse(String result) {


    }


//    @Override
//    protected BaseTurboCrawlerPageParser<TksMysteelCrawlerPageParser.PageVo> getPageParser() {
//        return new TksMysteelCrawlerPageParser();
//    }


    public static void main(String[] args) {
        TksMysteelCrawler balticExchangeCrawler = new TksMysteelCrawler();
        balticExchangeCrawler.crawl();
    }
}
